"use strict";
/* Controllers */
app.controller("roleConfigController", ['$scope', '$location', '$rootScope', '$q', '$routeParams', 'roleConfigService', 'userGroupService', function ($scope, $location, $rootScope, $q, $routeParams, roleConfigService, userGroupService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.p.tag = $routeParams.tag;
    // 默认值为1
    vm.p.action = 1;


    if(vm.p.tag=='id') {
        vm.p.id = $routeParams.id;
    }
    // 功能添加给用户
    if(vm.p.tag=='add_user') {
        vm.p.roleName = '用户';
        vm.p.id = 0;
        vm.p.functionLabels = $routeParams.id;
    }
    if(vm.p.tag=='add_group') {
        vm.p.roleName = '用户组';
        vm.p.id = 0;
        vm.p.functionLabels = $routeParams.id;
    }
    // 用户添加功能
    if(vm.p.tag=='user_id') {
        vm.p.roleName = '用户';
        vm.p.id = 0;
        vm.p.userIds = $routeParams.id;
    }
    if(vm.p.tag=='user_group') {
        vm.p.roleName = '用户组';
        vm.p.id = 0;
        vm.p.userGroupIds = $routeParams.id;
    }

    var errorFn = function (result) {
        alert(result);
    };

    $scope.loadRole = function () {
        var vm = $scope.vm;
        var params = vm.p;
        var procFn = function (res) {
            if (res.success && res.data) {
                vm.p = res.data
            } else {
                alert(res.msg);
            }
        };
        return roleConfigService.getRole(params, procFn, errorFn);
    };

    $scope.roleSave = function () {
        var vm = $scope.vm;
        var params = vm.p;
        var procFn = function (response) {
            if (response.success && response.data) {
                alert("保存成功！")
            } else {
                alert(response.msg);
            }
        };
        return roleConfigService.roleSave(params, procFn, errorFn);
    };

    if (typeof (vm.p.id) != "undefined" && vm.p.id != '0') {
        $scope.loadRole();
    }
}]);


/* Services */
app.service('roleConfigService', ['ajaxService', function (ajaxService) {

    this.roleSave = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/basic-service/v1/role/setRole.do", successFunction, errorFunction);
    };

    this.getRole = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/basic-service/v1/role/getRole.do", successFunction, errorFunction);
    };
}]);

